﻿using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursingUpperLimb
{
    public partial class HistoryUpperLimb : Form
    {
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;//当前的登录信息
        PatientInfo PatientInfo;
        public HistoryUpperLimb(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.LoginUser = loginUser;
            this.PatientInfo = patientInfo;
            BindControl();
            BindInfo();
        }

        private void BindControl()
        {
            StatDate.DateTime = DateTime.Now.AddDays(-30);
            EndDate.DateTime = DateTime.Now;
        }

        private void BindInfo()
        {
            string patientid = this.PatientInfo.PATIENT_ID;//住院号
            int visitId = PatientInfo.VISIT_ID;//住院次数
            int formId = (int)FormType.UpperLimbNursing;//18002  //表单ID
            DataTable dt = new DataTable();
            #region 列头
            dt.Columns.Add("PICCzhiguanshijian", typeof(string));
            dt.Columns.Add("PICCcaozuozhe", typeof(string));
            dt.Columns.Add("zhushou", typeof(string));
            dt.Columns.Add("daoguanleixing", typeof(string));
            dt.Columns.Add("guigexinghao", typeof(string));
            dt.Columns.Add("tiaoxingmapihao", typeof(string));
            dt.Columns.Add("daoruxueleixing", typeof(string));
            dt.Columns.Add("weixingdaoruxueguige", typeof(string));
            dt.Columns.Add("chauncishangzhizuo", typeof(string));
            dt.Columns.Add("chauncishangzhiyou", typeof(string));
            dt.Columns.Add("chauncixiazhizuo", typeof(string));
            dt.Columns.Add("chauncixiazhiyou", typeof(string));
            dt.Columns.Add("chaunciqita", typeof(string));
            dt.Columns.Add("charuchangdu", typeof(string));
            dt.Columns.Add("wailuchangdu", typeof(string));
            dt.Columns.Add("jianquchangdu", typeof(string));
            dt.Columns.Add("Xdingwei", typeof(string));
            dt.Columns.Add("ercidingwei", typeof(string));
            dt.Columns.Add("zaituichu", typeof(string));
            dt.Columns.Add("shijicharuchangdu", typeof(string));
            dt.Columns.Add("xindejianduanweizhi", typeof(string));
            dt.Columns.Add("caozuozhe", typeof(string));
            dt.Columns.Add("zhiguanjingguo", typeof(string));
            dt.Columns.Add("daiguanchuyuannian", typeof(string));
            dt.Columns.Add("daiguanchuyuanyue", typeof(string));
            dt.Columns.Add("daiguanchuyuanri", typeof(string));
            dt.Columns.Add("daiguanchuyuanshi", typeof(string));
            dt.Columns.Add("riqinian", typeof(string));
            dt.Columns.Add("riqiyue", typeof(string));
            dt.Columns.Add("riqiri", typeof(string));
            dt.Columns.Add("riqishi", typeof(string));
            dt.Columns.Add("baguancaozuozhe", typeof(string));
            dt.Columns.Add("baguanyuanyin1", typeof(string));
            dt.Columns.Add("baguanyuanyin2", typeof(string));
            dt.Columns.Add("baguanyuanyin3", typeof(string));
            dt.Columns.Add("baguanyuanyin4", typeof(string));
            dt.Columns.Add("baguanyuanyin5", typeof(string));
            dt.Columns.Add("baguanyuanyin6", typeof(string));
            dt.Columns.Add("baguanyuanyin7", typeof(string));
            dt.Columns.Add("baguanyuanyin8", typeof(string));
            dt.Columns.Add("baguanyuanyin9", typeof(string));
            dt.Columns.Add("baguanyuanyin10", typeof(string));
            dt.Columns.Add("baguanyuanyin11", typeof(string));
            dt.Columns.Add("baguanjingguo", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            #endregion
            var ret = formService.GetNursingFormRecordByDate(patientid, visitId, formId, StatDate.DateTime, EndDate.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    string theTrue = "√";
                    Model.UpperLimb fd = new Model.UpperLimb();
                    fd = XmlUtil.XMLToObject<Model.UpperLimb>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["PICCzhiguanshijian"] = fd.ZhiGuanJiLu.PICCZhiGuanShiJian;
                    newRow["PICCcaozuozhe"] = fd.ZhiGuanJiLu.PICCCaoZhe;
                    newRow["zhushou"] = fd.ZhiGuanJiLu.ZhuShou;
                    newRow["daoguanleixing"] = fd.ZhiGuanJiLu.DaoGuanLeiXing;
                    newRow["guigexinghao"] = fd.ZhiGuanJiLu.GuiGeXingHao;
                    newRow["tiaoxingmapihao"] = fd.ZhiGuanJiLu.TiaoXingMaPiHao;
                    newRow["daoruxueleixing"] = fd.ZhiGuanJiLu.DaoRuXiaoLeiXing;
                    newRow["weixingdaoruxueguige"] = fd.ZhiGuanJiLu.WeiXingDaoRuXiaoGuiGe;
                    newRow["chauncishangzhizuo"] = fd.ZhiGuanJiLu.ChuanCiBuWeiShangZhiZuo.Equals("0") ? theTrue : string.Empty;
                    newRow["chauncishangzhiyou"] = fd.ZhiGuanJiLu.ChuanCiBuWeiShangZhiYou.Equals("1") ? theTrue : string.Empty;
                    newRow["chauncixiazhizuo"] = fd.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiZuo.Equals("0") ? theTrue : string.Empty;
                    newRow["chauncixiazhiyou"] = fd.ZhiGuanJiLu.ChuanCiBuWeiXiaZhiYou.Equals("1") ? theTrue : string.Empty;
                    newRow["chaunciqita"] = fd.ZhiGuanJiLu.ChuanCiBuWeiQiTa;
                    newRow["charuchangdu"] = fd.ZhiGuanJiLu.ChaRuChangDu;
                    newRow["wailuchangdu"] = fd.ZhiGuanJiLu.WaiLuChangDu;
                    newRow["jianquchangdu"] = fd.ZhiGuanJiLu.JianQuChangDu;
                    newRow["Xdingwei"] = fd.ZhiGuanJiLu.XDingWei;
                    newRow["ercidingwei"] = fd.ZhiGuanJiLu.ErCiDingWei;
                    newRow["zaituichu"] = fd.ZhiGuanJiLu.ZaiTuiChu;
                    newRow["shijicharuchangdu"] = fd.ZhiGuanJiLu.ShiJiChaRuChangDu;
                    newRow["xindejianduanweizhi"] = fd.ZhiGuanJiLu.XinDeJianDuanWeiZhi;
                    newRow["caozuozhe"] = fd.ZhiGuanJiLu.CaoZuoZhe;
                    newRow["zhiguanjingguo"] = fd.ZhiGuanJiLu.ZhiGunJingGuo;
                    newRow["daiguanchuyuannian"] = fd.ZhiGuanJiLu.DaiGuanChuYuanShiJian.Split('/')[0];
                    newRow["daiguanchuyuanyue"] = fd.ZhiGuanJiLu.DaiGuanChuYuanShiJian.Split('/')[1];
                    newRow["daiguanchuyuanri"] = fd.ZhiGuanJiLu.DaiGuanChuYuanShiJian.Split('/')[2];
                    newRow["daiguanchuyuanshi"] = fd.ZhiGuanJiLu.DaiGuanChuYuanShiJian.Split('/')[3];
                    newRow["riqinian"] = fd.BaGuanJiLu.RiQi.Split('/')[0];
                    newRow["riqiyue"] = fd.BaGuanJiLu.RiQi.Split('/')[1];
                    newRow["riqiri"] = fd.BaGuanJiLu.RiQi.Split('/')[2];
                    newRow["riqishi"] = fd.BaGuanJiLu.RiQi.Split('/')[3];
                    newRow["baguanyuanyin1"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[0].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin2"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[1].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin3"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[2].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin4"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[3].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin5"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[4].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin6"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[5].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin7"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[6].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin8"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[7].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin9"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[8].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin10"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[9].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanyuanyin11"] = fd.BaGuanJiLu.BaGuanYuanYin.Split('#')[10].Equals("1") ? theTrue : string.Empty;
                    newRow["baguanjingguo"] = fd.BaGuanJiLu.BaGuanJingGuo;
                    //ID隐藏，双击触发事件
                    newRow["Id"] = record.Id;
                    dt.Rows.Add(newRow);
                }
            }
            gridControl1.DataSource = dt;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindInfo();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddUpperLimb addForm = new AddUpperLimb(LoginUser, PatientInfo, string.Empty);
            addForm.ShowDialog();
            if (addForm.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ShowEditForm();
        }

        private void ShowEditForm()
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddUpperLimb addForm = new AddUpperLimb(LoginUser, PatientInfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            UpperLimbPrints f = new UpperLimbPrints(PatientInfo, StatDate.DateTime, EndDate.DateTime);
            f.Show();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    BindInfo();
                }
            }
        }

        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            ShowEditForm();
        }
    }
}